<%@ page import="schoolsource.sql.SchoolDBSQL" %>
<%@ page import="schoolsource.gui.FormFields" %>
<%@ page import="schoolsource.model.TermContainer" %>
<%@ page import="schoolsource.model.Item" %>
<%@ page import="schoolsource.security.SecurityToken" %>
<%  
    SecurityToken st = (SecurityToken) request.getAttribute(FormFields.SESSION);
    String requestObject = (String) st.getValue(FormFields.TOKEN);
    TermContainer termContainer = (TermContainer) st.removeValue("termContainer"); 
    String editStatus = (String) st.removeValue(FormFields.EDITSTATUS);
    int editorPage = (new Integer((String) st.removeValue(FormFields.EDITORPAGE))).intValue();
    String displayMessage = (String) st.getValue("displayMessage"); 
    st.putValue("displayMessage", "");
	String loginType = (String) request.getAttribute(FormFields.USRTYPE); 
	String schoolName = (String) request.getAttribute(FormFields.SCHOOLNAME);
%>



<HTML>

<HEAD>

  <title>Term Editor Form</title>

</HEAD>

    <SCRIPT language="JavaScript" src="../jsp/calendar2.jsp"></SCRIPT>
    <SCRIPT LANGUAGE="JavaScript">

        function validateRequiredFields() {
            if (document.forms[0].<%= SchoolDBSQL.TERMNAME %>.value == "") {
                alert("You must enter a name for the term.");
                document.forms[0].<%= SchoolDBSQL.TERMNAME %>.focus();
                return false;
            }
            else {
                return true;
            }
        }

        function cancelForm() {
            document.forms[0].<%= FormFields.LINKCLICKED %>.value = "<%= FormFields.CANCEL %>";
            document.forms[0].submit();
        }

        function viewDetailedInfo(page, selectedItem) {
            top.frames.mainframe.document.forms[0].<%= FormFields.SUBLINK %>.value = selectedItem;
            top.frames.mainframe.submitPage(top.frames.mainframe.document, "<%= FormFields.EDITORPAGE %>", page);
            return false;
        }

        function postProcessing(x) {
            if (validateRequiredFields()) {
                document.forms[0].<%= FormFields.SAVETERM %>.value = x;
                document.forms[0].submit();
            }
        }

        function init() {
          if ("<%= displayMessage %>" != "null" && "<%= displayMessage %>" != "") {
            top.frames.mainframe.notify("<%= displayMessage %>");
          }
        }
  
    </SCRIPT>
    <body onLoad="init();
    	<% 
    	if (editStatus.equals(FormFields.VIEW) && termContainer != null) {
               out.print("top.frames.mainframe.highlightByID('" + termContainer.getTermAssignedId() + "');");
        }
        %>">

    <FORM NAME="termeditorform" METHOD="POST" onSubmit="return false;">
        <INPUT TYPE="HIDDEN" NAME="<%= SchoolDBSQL.TERMASSIGNEDID %>" <%= (st.removeValue("adding") == null) ? "onFocus=\"blur()\"" : "" %>
           VALUE="<%= (termContainer!=null) ? termContainer.getTermAssignedId().toString() : "nothing there"%>"> 
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.CURRENT %>" VALUE="<%= FormFields.SUBFRAME %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.TOKEN %>" VALUE="<%= requestObject%>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.PARAMETER %>" VALUE="<%= FormFields.EDTERMPAGE %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.LINKCLICKED %>" VALUE="<%= FormFields.SAVEACTION %>">
        <INPUT TYPE="HIDDEN" NAME="<%= FormFields.EDITORPAGE %>" VALUE="<%= editorPage %>"> 
		<INPUT TYPE="HIDDEN" NAME="<%= FormFields.SCHOOLNAME %>" VALUE="<%= schoolName %>">
		<INPUT TYPE="HIDDEN" NAME="<%= FormFields.USRTYPE %>" VALUE="<%= loginType %>">

        <H2>Term Editor</H2><br>
            <TABLE WIDTH="600">
            <TR>
                <TH ALIGN="LEFT">Term Name</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("                <INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.TERMNAME + "\" VALUE=\"" + termContainer.getTermName().toString() + "\">");
                        }
                        else {
                            out.println(termContainer.getTermName().toString());
                        }
                    %>
                </TD>
            </TR>

            <TR>
                <TH ALIGN="LEFT">Term Start</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("<INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.TERMSTART + "\" VALUE=\"" + termContainer.getTermStartDay().toString() + "\">");
                        	out.println("<a href=\"javascript:calTermStart.popup();\"><img src=\"../pics/cal.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Click Here to Pick up the date\"></a>&nbsp;(mm/dd/yyyy)");
                        }
                        else {
                            out.println(termContainer.getTermStartDay().toString());
                        }
                    %>
                </TD>
            </TR>
            <TR>
               <TH ALIGN="LEFT">Term End</TH>
                <TD>
                    <%
                        if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                            out.println("<INPUT TYPE=\"TEXT\" NAME=\"" + SchoolDBSQL.TERMEND + "\" VALUE=\"" + termContainer.getTermEndDay().toString() + "\">");
					        out.println("        <a href=\"javascript:calTermEnd.popup();\"><img src=\"../pics/cal.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Click Here to Pick up the date\"></a>&nbsp;(mm/dd/yyyy)");
                        }
                        else {
                            out.println(termContainer.getTermEndDay().toString());
                        }
                    %>
                </TD>
            </TR>

            <TR>
                <%
                    if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
                        out.println("                <TD><INPUT NAME=\"" + FormFields.SAVETERM  + "\" TYPE=\"BUTTON\" VALUE=\"Save\" onClick=\"postProcessing('Save')\"></TD>");
                        out.println("                <TD><INPUT NAME=\"cancel\" TYPE=\"BUTTON\" VALUE=\"Cancel\" onClick='cancelForm()'></TD>");
                    }
                %>
            </TR>

        </TABLE>
    <script language="JavaScript">
    <!-- // create calendar object(s) just after form tag closed
         // specify form element as the only parameter (document.forms['formname'].elements['inputname']);
         // note: you can have as many calendar objects as you need for your application

        <%
            if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
				out.println("        var calTermStart = new calendar2(document.forms['termeditorform'].elements['" + SchoolDBSQL.TERMSTART + "']);");
				out.println("        calTermStart.year_scroll = false;");
				out.println("        calTermStart.time_comp = false;");
            }
        %>
        
        <%
            if (editStatus.equals(FormFields.EDIT) || editStatus.equals(FormFields.NEW)) {
				out.println("        var calTermEnd = new calendar2(document.forms['termeditorform'].elements['" + SchoolDBSQL.TERMEND + "']);");
				out.println("        calTermEnd.year_scroll = false;");
				out.println("        calTermEnd.time_comp = false;");
            }
        %>

    //-->
    </script>

    </FORM>
</HTML>

